package com.gitee.btssplib

import android.bluetooth.BluetoothDevice
import android.util.Log

/**
 * 蓝牙管理器，负责管理蓝牙连接，读数等，是整个库使用的入口.
 * @author John
 * @version 1.0
 */
class BTManager {

    companion object {
        const val TAG = "BTManager"
    }

    var connectListener: ConnectListener? = null
    var receiveListener: ReceiveListener? = null
    var sendListener: SendListener? = null
    var receiveAfterConnected = false // 连接之后开始接收数据

    private val innerConnectListener: ConnectListener = object : ConnectListener {

        override fun onError(throwable: Throwable) {
            connectListener?.onError(throwable)
        }

        override fun onConnected() {
            // 连接后，开始心跳同时视情况而是否开启读数据
            heart.start()
            if (receiveAfterConnected) {
                receiver.receiveData()
            }
            connectListener?.onConnected()
        }

        override fun onStart() {
            // 开始连接，需要将心跳器和读数器停掉
            heart.stop()
            receiver.stop()
            connectListener?.onStart()
        }
    }

    private val innerReadDataListener: ReceiveListener = object : ReceiveListener {

        override fun onResult(data: Any) {
            receiveListener?.onResult(data)
        }

        override fun onError(throwable: Throwable) {
            // 读数发生异常而尝试重新连接
            receiveAfterConnected = true
            connector.reconnect()
            receiveListener?.onError(throwable)
        }

        override fun onStart() {
            receiveListener?.onStart()
        }
    }

    private val innerHeartListener: HeartListener = object : HeartListener {

        override fun onError(throwable: Throwable) {
            // 心跳发生异常则重新连接
            connector.reconnect()
        }

        override fun onStop() {
        }

        override fun onBeating(beatCount: Int) {
        }

        override fun onStart() {
        }
    }

    private val innerSendListener: SendListener = object : SendListener {

        override fun onError(throwable: Throwable) {
            sendListener?.onError(throwable)
        }

        override fun onSuccess(id: Long, data: ByteArray) {
            sendListener?.onSuccess(id, data)
        }
    }

    private val connector = BTConnector(innerConnectListener)
    private val receiver = BTReceiver(connector, innerReadDataListener)
    private val heart = BTHeart(connector, innerHeartListener)
    private val sender = BTSender(connector, innerSendListener)

    @Synchronized
    fun connect(device: BluetoothDevice) {
        connect(device, false)
    }

    @Synchronized
    fun connect(device: BluetoothDevice, readAfterConnected: Boolean) {
        this.receiveAfterConnected = readAfterConnected
        connector.connect(device)
    }

    @Synchronized
    fun startReceiveData() {
        if (connector.isConnected()) {
            Log.d(TAG, "蓝牙已连接，即将接收数据")
            receiver.receiveData()
        } else {
            Log.d(TAG, "蓝牙未连接，不可接收数据")
        }
    }

    @Synchronized
    fun stop() {
        heart.stop()
        sender.stop()
        receiver.stop()
        connector.stop()
    }

    @Synchronized
    fun sendData(data: ByteArray) {
        if (connector.isConnected()) {
            Log.d(TAG, "蓝牙已连接，即将发送数据")
            sender.send(data)
        } else {
            Log.d(TAG, "蓝牙未连接，不可发送数据")
        }
    }

    @Synchronized
    fun getDevice(): BluetoothDevice? {
        return connector.bluetoothDevice
    }

    @Synchronized
    fun setDataParser(dataParser: IDataParser<*>) {
        receiver.dataParser = dataParser
    }

    @Synchronized
    fun isConnected(): Boolean {
        return connector.isConnected()
    }

    @Synchronized
    fun isConnecting(): Boolean {
        return connector.isConnecting()
    }
}